<?php
/*
 * @美潮风
 * @功能说明：底层通讯协议
 * @更新说明：暂无更新
 * @文件名 ImTalk.class.php
 * @编码 UTF-8
 * @创建时间 2015-10-29 下午12:45:06
 * @创建人 XING
 */
namespace service\imtalk;

class ImTalk {
	/*网关地址 */
	public $service_port = 1935;
	/*序列号*/
	public $address = '121.40.53.78';

	/*
	 * 对象初始化
	 * @date: 2015-4-8下午3:07:04
	 * @editor: XING
	 */
	public function init(){
		$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
		if ($socket === false) {
			echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";exit;
		}
		
		return $socket;
	}

	/*
	 * 消息发送主体
	 * @date: 2015-4-8下午3:30:44
	 * @editor: XING
	 */
/* 	public function sendIm($status,$type,$str=""){
		$socket = self::init();
		$serverRespons = false;
		$result = socket_connect($socket, $this->address, $this->service_port);
		if($result === false) {
			echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
		} else {
		
		}
		$in = trim($str);
		
		$out = $data = "";
		socket_write($socket, $in, strlen($in));
		while ($out = socket_read($socket, 8192)) {
			$data .= $out;
		}
		socket_close($socket);
		
		file_put_contents('123.txt', $data);
		if(substr(trim($data),0,3) == '0x7E7'){
			$serverRespons = true;
		}
		
		return $serverRespons;
	} */
	public function sendIm($mac,$status,$type,$str=""){
		$setResult = S($mac,'0x7E'.$mac.$status.$type.iconv('utf-8','GBK',trim($str)).'0x7F'.chr(10));
		if($setResult)
			return true;
		else
			return false;
	}	
	/*
	 * 服务器端口监听
	 * @date: 2015-4-8下午3:30:44
	 * @editor: XING
	 */
	public function receiveIm(){
		$socket = self::init();
		if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
		    echo 'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
		}
		
		if($socket < 0) {
			echo "Socket create:error\n";
			exit;
		}
		
		if (! ($ret = socket_bind($socket, $this->address, $this->service_port)) ) {
			$cmd =isset($_SERVER['WINDIR'])? "netstat -an -o | find \"".$this->service_port."\"":"netstat -anp | grep \"".$this->service_port."\"";
			$output = NULL;
			exec( $cmd, $output , $return_var);
			echo "
			-----------------------------------------------		
			$cmd		
			<pre>";
			var_dump($output, $return_var);
			echo "/<pre>";
		    //echo "//AS!\n";
		    exit;
		}
		
		if ( ($ret = socket_listen($socket, 5)) < 0 ) {
		    echo "socket listen:".socket_strerror()."\n";
		    exit;
		}
		
		socket_set_nonblock($socket);
		
		$clients = array($socket);
		while(true) {
			$read = $clients;
			$writes=NULL;$execs=NULL;
			if(socket_select($read, $writes, $execs, 2) < 1){		
				echo ".";
				continue;
			}
			
			if(in_array($socket,$read)) {
				$newsock = socket_accept($socket);
				socket_set_nonblock($newsock);
				socket_getpeername($newsock,$remoteIP,$remotePort);		
				$userSN = $remoteIP.'_'.$remotePort;
			//	echo $userSN.' connetect in'.chr(10);
				$clients[$userSN] = $newsock;
				socket_write($newsock,'welcome '.$userSN.chr(10));
				continue;
			}
		    
		    foreach($read as $read_sock) {
				$data = @socket_read($read_sock, 2048, PHP_BINARY_READ);
				$data = trim($data);
				if(strlen($data) == 2048){
					socket_close($read_sock);
					continue;
				}
				if(S('waitLine')){
					$data = trim(S('waitLine'));
					S('waitLine',null);
				}
				@socket_getpeername($read_sock, $remoteIP, $remotePort);	
				$userSN = $remoteIP.'_'.$remotePort;
		        //echo $userSN.' say:'.$data.chr(10);
				socket_write($read_sock,'you say:'.$data.$userSN);
		    }
		}
	}

}


?>